import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router/src/utils/preactivation';
import { ActivatedRouteSnapshot, Router, RouterStateSnapshot } from '@angular/router';
import { BehaviorSubject } from 'rxjs';
import { LicenseInfo } from 'app/modules/settings/services/settings.service';
import { MatDialog } from '@angular/material';
import { SettingsService } from '../../modules/settings/services/settings.service';
import { LoginService } from '../../modules/login/services/login.service';


@Injectable({
  providedIn: 'root',
})
export class LicenseGuard implements CanActivate {

  constructor(
    public router: Router,
    private settingsService: SettingsService,
    private loginService: LoginService
    ) {}

  path: ActivatedRouteSnapshot[];
  route: ActivatedRouteSnapshot;
	licenseInfo$: BehaviorSubject<LicenseInfo>;
  async canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    const user = await this.loginService.User();
    this.settingsService.get().then((data: any) => {
      if (this.settingsService.licenseInfo===undefined) {
        this.settingsService.updateLicenseInfo({profile: data});
        return false;
      }
      if (user.uid == 1 && state.url === '/settings/info') {
        return true;
      }
      const expired = data.expired_at === 0 ? false : new Date(data.expired_at*1000) < new Date();
      if (expired) {
        this.settingsService.updateLicenseInfo({profile: data});
        // this.router.navigateByUrl('/');
        return false
      }
    })
    return true;
  }
  
}
